<?php
class SearchableHelper extends AppHelper {
    public $helpers = array('Html', 'Text');

    /* Parse result data for later HTML generating */
    public function parseResult($data){
        $result = array();
        $raw = explode('|||', $data['data']);
        $term = $this->data['SearchIndex']['term'];
        foreach($raw as $key=>$value){
            if(strpos($value, '||') !== false){
                $pair = explode('||', $value);
                //$result[trim($pair[0])] = $this->Text->truncate($this->Text->highlight(trim($pair[1]), $term), 200);
                $content = str_replace('&nbsp;', '', trim($pair[1]));
                $content = $this->Text->truncate($content, 200);
                foreach($term as $tm){
                    //$content = call_user_func(array($this->Text, 'hightlight'), $content, $tm);
                    $content = $this->Text->highlight($content, $tm);
                }
                $result[trim($pair[0])] = $content;
            }
        }
        unset($data['data'], $raw);
        $data = array_merge($result, $data);
        $method = '__generate' . $data['model'];
        $data['url'] = (array) json_decode($data['url']);
        return $this->{$method}($data);
    }

    /* Generate html markups based on the parsed data */
    private function __generateComment($data){
        return $data;
    }
    private function __generateCompany($data){
        $content = '<h3 class="link '.strtolower($data['model']).'">';
        $content .=  $this->Html->link($data['Company.name'], $data['url'], array(), false, false);
        $content .= '</h3>';
        $content .= '<p class="meta">';
        $content .= '<span class="meta location">'.__('CompanyLocation', true).': '.$data['CompanyLocation.name'].'</span>';
        $content .= '<span class="meta industry">'.__('CompanyIndustry', true).': '.$data['CompanyIndustry.name'].'</span>';
        $content .= '<span class="meta type">'.__('CompanyType', true).': '.$data['CompanyType.name'].'</span>';
        $content .= '</p>';
        $content .= '<p class="abstract">';
        $content .= $data['Company.introduction'];
        $content .= '</p>';
        return $content;
    }
    private function __generateJob($data){
        $content = '<h3 class="link '.strtolower($data['model']).'">';
        $content .= $this->Html->link($data['Job.title'], $data['url'], array(), false, false);
        $content .= '</h3>';
        $content .= '<p class="meta">';
        $content .= '<span class="meta workplace">'.__('Workplace', true).': '.$data['Job.workplace'].'</span>';
        $content .= '<span class="meta company">'.__('Company', true).': '.$data['Company.name'].'</span>';
        $content .= '</p>';
        $content .= '<p class="abstract">';
        $content .= $data['Job.introduction'];
        $content .= $data['Job.requirement'];
        $content .= '</p>';
        return $content;
    }
    private function __generatePost($data){
        $content = '<h3 class="link '.strtolower($data['model']).'">';
        $content .= $this->Html->link($data['Post.title'], $data['url'], array(), false, false);
        $content .= '</h3>';
        $content .= '<p class="meta">';
        $content .= '<span class="meta term">'.__('PostTerm', true).': '.$data['PostTerm.name'].'</span>';
        $content .= '</p>';
        $content .= '<p class="abstract">';
        $content .= $data['Post.content'];
        $content .= '</p>';
        return $content;
    }
    private function __generateStudent($data){
        $gender = $data['Student.gender'] == '男' ? 'male' : 'female';
        $content = '<h3 class="link '.strtolower($data['model']).' '.$gender.'">';
        $content .=  $this->Html->link($data['Student.name'], $data['url'], array(), false, false);
        $content .= '</h3>';
        $content .= '<p class="abstract">';
        $content .= '<span class="meta number">'.__('Number', true).': '.$data['Student.number'].'</span>';
        $content .= '<span class="meta class">'.__('Class', true).': '.$data['Student.class'].'</span>';
        $content .= '<span class="meta degree">'.__('Degree', true).': '.$data['Student.degree'].'</span>';
        $content .= '<span class="meta degree">'.__('Academic', true).': '.$data['Student.academic'].'</span>';
        $content .= '<span class="meta political">'.__('Political', true).': '.$data['Student.political'].'</span>';
        $content .= '<span class="meta minority">'.__('StudentMinority', true).': '.$data['StudentMinority.name'].'</span>';
        $content .= '<span class="meta province">'.__('StudentProvince', true).': '.$data['StudentProvince.name'].'</span>';
        $content .= '</p>';
        return $content;
    }
}
?>
